<template>
  <div class="table-page">
    <GiTable title="试验工作量管理" row-key="id" :data="dataList" :columns="columns" :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }" :pagination="pagination" :disabled-tools="['size']"
      :disabled-column-keys="['name']" @refresh="search">
      <template #toolbar-left>
        <a-date-picker v-model="queryForm.startTime" placeholder="请选择开始时间" format="YYYY-MM-DD" style="height: 32px" />
        <a-date-picker v-model="queryForm.endTime" placeholder="请选择结束时间" format="YYYY-MM-DD" style="height: 32px" />
        <a-input v-model="queryForm.testHour" placeholder="请输入试验时数" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-input v-model="queryForm.overtimeHour" placeholder="请输入加班时数" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-select v-model="queryForm.registerStatus" :options="register_type" placeholder="请选择登记项目" allow-clear
          style="width: 150px" @change="search" />
        <a-select v-model="queryForm.isFault" :options="fault_status" placeholder="请选择发动机是否故障" allow-clear
          style="width: 150px" @change="search" />
        <a-input v-model="queryForm.name" placeholder="请输入试验名称" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #toolbar-right>
        <a-button v-permission="['task:testLog:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button>
        <a-button v-permission="['task:testLog:export']" @click="onExport">
          <template #icon><icon-download /></template>
          <template #default>导出</template>
        </a-button>
      </template>
      <template #registerStatus="{ record }">
        <GiCellTag :value="record.registerStatus" :dict="register_type" />
      </template>
      <template #isFault="{ record }">
        <GiCellTag :value="record.isFault" :dict="fault_status" />
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['task:testLog:detail']" title="详情" @click="onDetail(record)">详情</a-link>
          <a-link v-permission="['task:testLog:update']" title="修改" @click="onUpdate(record)">修改</a-link>
          <a-link v-permission="['task:testLog:delete']" status="danger" :disabled="record.disabled"
            :title="record.disabled ? '不可删除' : '删除'" @click="onDelete(record)">
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <TestLogAddModal ref="TestLogAddModalRef" @save-success="search" />
    <TestLogDetailDrawer ref="TestLogDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import TestLogAddModal from './TestLogAddModal.vue'
import TestLogDetailDrawer from './TestLogDetailDrawer.vue'
import { useDownload, useTable } from '@/hooks'
import { useDict } from '@/hooks/app'
import { isMobile } from '@/utils'
import has from '@/utils/has'
import {
  type TestLogResp,
  type TestLogQuery,
  deleteTestLog,
  exportTestLog,
  listTestLog
} from '@/apis/task/testLog'
import type { TableInstanceColumns } from '@/components/GiTable/type'

defineOptions({ name: 'TestLog' })

const { register_type, fault_status } = useDict('register_type', 'fault_status')

const queryForm = reactive<TestLogQuery>({
  startTime: undefined,
  endTime: undefined,
  testHour: undefined,
  overtimeHour: undefined,
  registerStatus: undefined,
  isFault: undefined,
  name: undefined,
  sort: ['id,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listTestLog({ ...queryForm, ...page }), { immediate: true })
const columns: TableInstanceColumns[] = [
  { title: '任务名称', dataIndex: 'taskName', slotName: 'taskName' },
  // { title: 'ID', dataIndex: 'id', slotName: 'id' },
  { title: '试验人员', dataIndex: 'logUserNames', slotName: 'logUserNames' },
  { title: '开始时间', dataIndex: 'startTime', slotName: 'startTime' },
  { title: '结束时间', dataIndex: 'endTime', slotName: 'endTime' },
  { title: '试验时数', dataIndex: 'testHour', slotName: 'testHour' },
  { title: '加班时数', dataIndex: 'overtimeHour', slotName: 'overtimeHour' },
  { title: '登记项目', dataIndex: 'registerStatus', slotName: 'registerStatus' },
  { title: '发动机是否故障', dataIndex: 'isFault', slotName: 'isFault' },
  { title: '故障描述', dataIndex: 'faultDescription', slotName: 'faultDescription' },
  { title: '试验名称', dataIndex: 'testName', slotName: 'testName' },
  { title: '试验情况简述', dataIndex: 'testDescription', slotName: 'testDescription' },
  { title: '设备地点名称', dataIndex: 'locName', slotName: 'locName' },
  {
    title: '操作',
    dataIndex: 'action',
    slotName: 'action',
    width: 160,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['task:testLog:detail', 'task:testLog:update', 'task:testLog:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.startTime = undefined
  queryForm.endTime = undefined
  queryForm.testHour = undefined
  queryForm.overtimeHour = undefined
  queryForm.registerStatus = undefined
  queryForm.isFault = undefined
  queryForm.name = undefined
  search()
}

// 删除
const onDelete = (record: TestLogResp) => {
  return handleDelete(() => deleteTestLog(record.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

// 导出
const onExport = () => {
  useDownload(() => exportTestLog(queryForm))
}

const TestLogAddModalRef = ref<InstanceType<typeof TestLogAddModal>>()
// 新增
const onAdd = () => {
  TestLogAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (record: TestLogResp) => {
  TestLogAddModalRef.value?.onUpdate(record.id)
}

const TestLogDetailDrawerRef = ref<InstanceType<typeof TestLogDetailDrawer>>()
// 详情
const onDetail = (record: TestLogResp) => {
  TestLogDetailDrawerRef.value?.onOpen(record.id)
}
</script>

<style scoped lang="scss"></style>
